/*
* Copyright (c) 2025 Shenzhen Kaihong Digital Industry Development Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
const { XButton } = require("../engine/control/XButton");
const { X2DBeta } = require("../engine/graphics/X2DInterface");
const { Scr } = require("../engine/XDefine");
const { XTools } = require("../engine/XTools");
const { XStat } = require("../engine/XStat");
const { XWindow } = require("../engine/control/XWindow");
const { WsClient2 } = require("../engine/ws/WsClient2");
const { PackageTool } = require("./ros_msg_utils/PackageTool");
const { XInput } = require("../engine/control/XInput");
const { EditRos1Project } = require("./ros1/EditRos1Project");
const { ProjectConfig } = require("./ros1/ProjectConfig");

class Page3SelectPrj extends XWindow {
    constructor() {
        super("Page3SelectPrj", false);
        this.projectBtns = [];
        WsClient2.registProcess("get_project_list_ret", (pt_ret) => {
            let projectList = pt_ret.get_string_array();
            this.projectBtns = [];
            for (let i = 0; i < projectList.length; i++) {
                let prj_name = projectList[i];
                let btn = new XButton({
                    name: prj_name,
                    x: 300,
                    y: 100 + i * 40,
                    w: 300,
                    h: 30,
                    onClicked: () => {
                        if (G.ROS_NAME == "ROS1") {
                            G.PROJECT = {};
                            ProjectConfig.PROJECT_NAME = prj_name;
                            EditRos1Project.open();
                        }
                    }
                })
                this.projectBtns.push(btn);
                this.addUI(btn);
            }
        });
        WsClient2.registProcess("create_project_ret", (pt_ret) => {
            if (pt_ret.get_bool()) {//创建工程成功
                // G.PROJECT_PATH = pt_ret.get_string();//直接打开工程
                this.getProjectList();
            } else {
                console.log(pt_ret.get_string());
            }
        });
        this.getProjectList();

        this.input_new_prj = new XInput({
            prompt: "输入工程名",
            x: 100,
            y: Scr.logich - 50,
            w: 200,
            h: 30,
        })
        this.addUI(this.input_new_prj)
        this.addUI(new XButton({
            name: "创建新工程",
            x: 300,
            y: Scr.logich - 50,
            w: 200,
            h: 20,
            onClicked: () => {
                //检查工程名是否合法
                if (this.input_new_prj.name_.length < 4) {
                    return;
                }
                if (this.input_new_prj.name_.length > 32) {
                    return;
                }
                const regex = /^[A-Za-z][A-Za-z0-9]*$/;
                if (!regex.test(this.input_new_prj.name_)) {
                    return;
                }
                let pt = new PackageTool();
                pt.put_string("create_project");
                pt.put_string(ProjectConfig.WORKSPACE_PATH);
                pt.put_string(this.input_new_prj.name_);
                pt.put_string(JSON.stringify({
                    "name": this.input_new_prj.name_,
                    "version": "0.0.1",
                }))
                WsClient2.send(pt);
            }
        }));
    }
    getProjectList() {
        let pt = new PackageTool();
        pt.put_string("get_project_list");
        pt.put_string(ProjectConfig.WORKSPACE_PATH)
        WsClient2.send(pt);
    }
    onDraw() {
        super.onDraw();
        X2DBeta.drawText("当前工程数量：" + this.projectBtns.length, 24, 300, 10, 1, 1, 0, -1, -1, 0xffffffff);
        if (XTools.AUTO_LOGIN && this.projectBtns.length > 0) {
            this.projectBtns[0].clickCallback_();
        }
    }
    onTouch(msg, x, y) {
        if (super.onTouch(msg, x, y)) {
            return true;
        }
        return false;
    }
    onKey(type, k) {
        return true;
    }
    static open() {
        XStat.pushStat(new Page3SelectPrj());
    }
}

module.exports = {
    Page3SelectPrj
}